package com.ittx.dzt.baidu.service;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Maps;
import com.ittx.dzt.baidu.constant.BaiduApiConstant;
import com.ittx.dzt.baidu.constant.CommonConstant;
import com.ittx.dzt.baidu.dto.AreaData;
import com.ittx.dzt.baidu.dto.AreaDataDto;
import com.ittx.dzt.baidu.handler.AbstractBaseBaiduApiHandler;
import com.ittx.dzt.business.PlatformConstant;
import com.ittx.dzt.business.constant.RocketmqTopicConstant;
import com.ittx.dzt.edi.constant.GeneralConstant;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.sys.response.Result;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 平台地址库下载
 * 按照最小行政区域地址逐条下发
 * <p>
 * 查询区域地址（查询省、市、区/县、社区/街道列表） /PlatRegionStoreService/getAreaList
 * cy.area.download.splitting
 * https://dev2.baidu.com/content?sceneType=0&pageId=104766&nodeId=1539&subhead=
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/2/28
 */
@Slf4j
@Service
public class BaiduAreaDownloadSpService extends AbstractBaseBaiduApiHandler<String> {

	@Autowired
	private BaiduGetAreaListService baiduGetAreaListService;

	@Override
	public String getLocalApiMethod() {
		return PlatformApiConstant.InboundInterface.AREA_DOWNLOAD_SPLITTING;
	}

	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, String s) {
		// 获取所有省份(空map)
		Result<?> result = baiduGetAreaListService.doProcess(platformApiParamDTO, Maps.newHashMap());
		if (!result.isSuccess()) {
			return result;
		}
		List<Map<String, Object>> provinceList = (List<Map<String, Object>>) result.getData();
		//初始化一个国家
		AreaData areaData = new AreaData();
		areaData.setCountry(GeneralConstant.AreaDefault.COUNTRY);
		areaData.setName(GeneralConstant.AreaDefault.COUNTRY);
		areaData.setFullAddress(GeneralConstant.AreaDefault.COUNTRY);
		areaData.setCode(GeneralConstant.AreaDefault.COUNTRY_CODE);
		areaData.setLevel(CommonConstant.AreaLevelSmall.COUNTRY);
		areaData.setParent(GeneralConstant.AreaDefault.COUNTRY_PARENT);
		areaData.setPlatformCode(PlatformConstant.BAIDU);
		// 异步发给数据中心
		result = sendRequestToPlatform(platformApiParamDTO, areaData, true);
		if (result.hasError()) {
			return result;
		}
		for (Map<String, Object> province : provinceList) {
			AreaDataDto areaDataDto = new AreaDataDto();
			String provinceId = province.get("code").toString();
			areaDataDto.setProvinceId(provinceId);
			areaDataDto.setAreaData(areaData);
			areaDataDto.setPlatformApiParamDTO(platformApiParamDTO);
			// 异步发给MQ
			rocketmqProducer.sendAsyncMsg(BaiduApiConstant.Rocket.BAIDU_AREA_DOWN_TOPIC, RocketmqTopicConstant.DataCenter.DATA_DEFAULT_TAG,
					areaDataDto, provinceId);
		}
		return Result.success();
	}


	/**
	 * 逐条拆分发送数据中心
	 */
	public void sendAreasToDataCenter(PlatformApiParamDTO platformApiParamDTO, List<Map<String, Object>> dataItemList, AreaData area) {
		if (CollectionUtil.isEmpty(dataItemList)) {
			return;
		}
		try {
			//dataItemList = {code:"",children:[],name:""}
			for (Map<String, Object> dataItem : dataItemList) {
				AreaData areaData = new AreaData();
				areaData.setPlatformCode(PlatformConstant.BAIDU);
				areaData.setParent(area.getCode());
				// 将父节点所属省市区写到子节点
				areaData.setCountry(area.getCountry());
				areaData.setState(area.getState());
				areaData.setCity(area.getCity());
				areaData.setDistrict(area.getDistrict());
				areaData.setStreet(area.getStreet());
				areaData.setCode(String.valueOf(dataItem.get("code")));
				areaData.setName(String.valueOf(dataItem.get("name")));
				//根据父节点级别定义子节点级别，并给子节点对应的级别加上name
				switch (area.getLevel()) {
					case CommonConstant.AreaLevelSmall.COUNTRY:
						areaData.setLevel(CommonConstant.AreaLevelSmall.STATE);
						areaData.setState(areaData.getName());
						break;
					case CommonConstant.AreaLevelSmall.STATE:
						areaData.setLevel(CommonConstant.AreaLevelSmall.CITY);
						areaData.setCity(areaData.getName());
						break;
					case CommonConstant.AreaLevelSmall.CITY:
						areaData.setLevel(CommonConstant.AreaLevelSmall.DISTRICT);
						areaData.setDistrict(areaData.getName());
						break;
					case CommonConstant.AreaLevelSmall.DISTRICT:
						areaData.setLevel(CommonConstant.AreaLevelSmall.STREET);
						areaData.setStreet(areaData.getName());
						break;
					default:
						break;
				}
				areaData.setFullAddress(area.getFullAddress() + "-" + areaData.getName());
				// 发送给MQ 让MQ发给数据中心
				areaData.setPlatformApiParamDTO(platformApiParamDTO);
				rocketmqProducer.sendAsyncMsg(BaiduApiConstant.Rocket.BAIDU_SEND_AREA_TOPIC, RocketmqTopicConstant.DataCenter.DATA_DEFAULT_TAG,
						areaData, areaData.getCode());
				//（递归）解读此新地址的子节点
				List<Map<String, Object>> subDistricts = (List<Map<String, Object>>) dataItem.get("children");
				sendAreasToDataCenter(platformApiParamDTO, subDistricts, areaData);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public String getBaiduMethod() {
		return "/PlatRegionStoreService/getAreaList";
	}
}
